/*********************************************************************************
 *      Copyright:  (C) 2022 Nikel
 *                  All rights reserved.
 *
 *       Filename:  optimget.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(05/05/22)
 *         Author:  zhouyanlin <www.zhouyanlin1222@qq.com>
 *      ChangeLog:  1, Release initial version on "05/05/22 08:52:15"
 *                 
 ********************************************************************************/

#include "conf.h"

#define INI_PATH "./app.ini"

#define ALI 1
#define TENGXUN 2
#define HUAWEI 3

int init_config_mqtt(sqlite_ctx_t *sqlite_ctx, mqtt_ctx_t *mqtt_ctx, pack_ctx_t *json, int platform)
{
	if(!mqtt_ctx || !json || !platform)
	{
		printf ("Incorrect input parameter!\n");
		return -1;
	}

	dictionary * initfd = NULL; 

	if( NULL == (initfd = iniparser_load(INI_PATH)) )
	{
		printf ("iniparser_load failure!\n");
		return -2;
	}

	if(ALI == platform)
	{
		mqtt_ctx->host = iniparser_getstring(initfd, "aly_conn_ctx:host", "NULL"); 
		mqtt_ctx->port = iniparser_getint(initfd, "aly_conn_ctx:port", 0); 
		mqtt_ctx->username = iniparser_getstring(initfd, "aly_conn_ctx:username", "NULL");
		mqtt_ctx->passward = iniparser_getstring(initfd, "aly_conn_ctx:passward", "NULL");
		mqtt_ctx->clientid = iniparser_getstring(initfd, "aly_conn_ctx:clientid", "NULL");
		mqtt_ctx->topic = iniparser_getstring(initfd, "aly_conn_ctx:topic", "NULL");

		json->method = iniparser_getstring(initfd, "aly_pack_ctx:method", "NULL");
		json->id = iniparser_getstring(initfd, "aly_pack_ctx:id", "NULL");
		json->payload = iniparser_getdouble(initfd, "aly_pack_ctx:payload", 0.00);
		json->version = iniparser_getstring(initfd, "aly_pack_ctx:Version", "NULL");
		return 0;

	}

	else if(TENGXUN == platform)
	{
		mqtt_ctx->host = iniparser_getstring(initfd, "txy_conn_ctx:host", "NULL"); 
		mqtt_ctx->port = iniparser_getint(initfd, "txy_conn_ctx:port", 0); 
		mqtt_ctx->username = iniparser_getstring(initfd, "txy_conn_ctx:username", "NULL");
		mqtt_ctx->passward = iniparser_getstring(initfd, "txy_conn_ctx:passward", "NULL");
		mqtt_ctx->clientid = iniparser_getstring(initfd, "txy_conn_ctx:clientid", "NULL");
		mqtt_ctx->topic = iniparser_getstring(initfd, "txy_conn_ctx:topic", "NULL");

		json->method = iniparser_getstring(initfd, "txy_pack_ctx:method", "NULL");
		json->id = iniparser_getstring(initfd, "txy_pack_ctx:id", "NULL");
		json->payload = iniparser_getdouble(initfd, "txy_pack_ctx:payload", 0.00);
		json->version = iniparser_getstring(initfd, "txy_pack_ctx:Version", "NULL");
		return 0;

	}

	else if(HUAWEI == platform)
	{
		mqtt_ctx->host = iniparser_getstring(initfd, "hwy_conn_ctx:host", "NULL"); 
		mqtt_ctx->port = iniparser_getint(initfd, "hwy_conn_ctx:port", 0); 
		mqtt_ctx->username = iniparser_getstring(initfd, "hwy_conn_ctx:username", "NULL");
		mqtt_ctx->passward = iniparser_getstring(initfd, "hwy_conn_ctx:passward", "NULL");
		mqtt_ctx->clientid = iniparser_getstring(initfd, "hwy_conn_ctx:clientid", "NULL");
		mqtt_ctx->topic = iniparser_getstring(initfd, "hwy_conn_ctx:topic", "NULL");

		json->id = iniparser_getstring(initfd, "hwy_pack_ctx:id", "NULL");
		json->payload = iniparser_getdouble(initfd, "hwy_pack_ctx:payload", 0.00);
		return 0;

	}
}

int init_config_sqlite(sqlite_ctx_t *sqlite_ctx)
{
	if(!sqlite_ctx)
	{
		printf("Invalid input parameter in init_config_sqlite function\n");
		return -1;
	}

	dictionary * initfd = NULL; 

	if( NULL == (initfd = iniparser_load(INI_PATH)) )
	{
		printf ("iniparser_load failure!\n");
		return -2;
	}

	sqlite_ctx->database_name = iniparser_getstring(initfd, "sqlite_ctx:database_name", "NULL");
	sqlite_ctx->table_name = iniparser_getstring(initfd, "sqlite_ctx:table_name", "NULL");
	return 0;

}

int init_config_total(app_ctx_t *app_ctx)
{
	
	if(!app_ctx)
	{
		printf ("Incorrect input parameter in %s \n", __FUNCTION__ );
		return -1;
	}

	/*  if run at deamon */
	if( app_ctx->my_daemon )
	{
		log_info("The process is going to running at daemon!\n");
		printf("The process is going to running at daemon!\n");
		if( daemon(1, 0) < 0)
		{
			log_error("Program daemon() failure: %s\n",strerror(errno));
			printf("Program daemon() failure: %s\n",strerror(errno));
			return -2;
		}

	}

	if( init_config_sqlite(&app_ctx->sqlite_ctx) < 0 )
	{
		log_error("init_config_sqlite function call failure!\n");
		return -3;
	}

	if( init_config_mqtt(&app_ctx->sqlite_ctx, &app_ctx->mqtt_ctx, &app_ctx->pack_ctx, app_ctx->mqtt_ctx.platform) < 0 )
	{
		log_error("init_config_mqtt function call failure!\n");
		return -4;
	}

	/* open my log */
	if( open_my_log(app_ctx->debug) < 0)
	{
		log_error("open_my_log function call failure\n");
		return -5;
	}

	/* create_database and database'table */
	if( create_database_and_table(app_ctx->sqlite_ctx) < 0)
	{
		log_error("create_database_and_table function call failure\n");
		return -6;
	}

#if 0
	/* mosquitto_init */
	if( mosquitto_init(&app_ctx->mqtt_ctx) <0 )
	{
		log_error("mosquitto_init function call failure!\n");
		return -7;
	}
#endif

	/* install the signal */
	signal(SIGTERM, sig_handle); /*  for SIGTERM to print message befor quit program */
	signal(SIGINT, sig_handle);	/*  for SIGTERM to print message befor quit program */

#if 0
		printf ("going out init_config_mqtt \n");
		printf("app_ctx->sqlite_ctx->database_name:%s\n", app_ctx->sqlite_ctx.database_name);
		printf("app_ctx->sqlite_ctx->database_name:%s\n", app_ctx->sqlite_ctx.table_name);
	    printf ("topic:%s\n", app_ctx->mqtt_ctx.topic);
		printf ("host:%s\n", app_ctx->mqtt_ctx.host);
		printf ("username:%s\n", app_ctx->mqtt_ctx.username);
		printf ("port :%d\n", app_ctx->mqtt_ctx.port);
		printf ("passward:%s\n", app_ctx->mqtt_ctx.passward);
		printf ("clientid:%s\n\n\n", app_ctx->mqtt_ctx.clientid);
#endif

	return 0;
}

#if 0
		printf ("going out init_config to main!\n");
		printf("app_ctx.database_name:%s\n", app_ctx.sqlite_ctx.database_name);
		printf("app_ctx.database_name:%s\n", app_ctx.sqlite_ctx.table_name);
	    printf ("topic:%s\n", app_ctx.mqtt_ctx.topic);
		printf ("host:%s\n", app_ctx.mqtt_ctx.host);
		printf ("username:%s\n", app_ctx.mqtt_ctx.username);
		printf ("port :%d\n", app_ctx.mqtt_ctx.port);
		printf ("passward:%s\n", app_ctx.mqtt_ctx.passward);
		printf ("clientid:%s\n\n\n", app_ctx.mqtt_ctx.clientid);
#endif
